/**
 * Created by Administrator on 2015/10/21 0021.
 */

/**
 * 我的站内信 设置
 */

define(function(require, exports, module){
    /*类型设置*/
    var UI = require("js/UI");

    //下拉选择
    var $messageSelect = $("#listFilterForm .messageSelect");
    UI.dropDownSelect($messageSelect);

    var $homeMenu = $("#listFilterForm .homeMenu");
    var $group = $("#listMessage .group");
    var $flt = $("#listFilterForm .flt");

    //初始化加载 加载全部信息
    UI.ajax("/json/message.json","get",{}, function (flag,json) {
        fnMessage(json);
    });

    //全部、未读、已读切换
    var $a = $homeMenu.find("a").unbind("click.a").bind("click.a",function(){
        var $this = $(this);
        $a.removeClass("curr");
        $this.addClass("curr");

        switch ($this.index()){
            case 0:   //全部
                UI.ajax("/json/message.json","get",{}, function (flag,json) {
                    fnMessage(json);
                });
                break;
            case 1: //未读
                UI.ajax("/json/message.json","get",{}, function (flag,json) {
                    fnMessage(json);
                });
                break;
            case 2:  //已读
                UI.ajax("/json/message.json","get",{}, function (flag,json) {
                    fnMessage(json);
                });
                break;
        }

    });

    function fnMessage(data){
        //填充表单
        var $temp = $("<div></div>");
        for(var i = 0; i < data.length; i++){
            var $trstr = $("<div class='tr'></div>");
            $("<div class='td'></div>").addClass("t" + 1).text(data[i].state).appendTo($trstr);
            $("<div class='td'></div>").addClass("t" + 2).text(data[i].clas).appendTo($trstr);
            $("<div class='td'></div>").addClass("t" + 3).text(data[i].tit).appendTo($trstr);
            $("<div class='td'></div>").addClass("t" + 4).text(data[i].time).appendTo($trstr);
            $("<div class='td'></div>").addClass("t" + 5).append("<a class='btn' href='#'>查看详情</a><a class='btn delete' href='#'>刪除</a>").appendTo($trstr);
            $temp.append($trstr);
        }
        $group.empty().append($temp.children());
        //填充表单end

        //点击tr选中
        var $tr = $group.find(".tr").unbind("click.tr").bind("click.tr",function(){
            var $this = $(this);
            if($this.hasClass("selected")){
                $this.removeClass("selected");
            }else{
                $this.addClass("selected");
            }
        });

        //删除tr
        var $delete = $tr.children(".t5").find(".delete").unbind("click.delete").bind("click.delete",function(){
            $(this).parents(".tr").remove();
            location.reload();
        })

        //全选
        var $allSelect = $flt.find(".btn").eq(0).unbind("click.allSelect").bind("click.allSelect",function(){
            $tr.addClass("selected");
        })

        //标记所选为已读
        var $read = $flt.find(".btn").eq(1).unbind("click.allSelect").bind("click.allSelect",function(){
            for(var i=0;i<$tr.length;i++){
                var _tr = $tr.eq(i);
                if(_tr.hasClass("selected")){
                    location.reload();
                }
            }
        })

        //删除所选
        var $delete = $flt.find(".btn").eq(2).unbind("click.allSelect").bind("click.allSelect",function(){

            for(var i=0;i<$tr.length;i++){
                var _tr = $tr.eq(i);
                if(_tr.hasClass("selected")){
                    _tr.remove();
                }
            }
        })

        //清空所有
        var $remove = $flt.find(".btn").eq(3).unbind("click.allSelect").bind("click.allSelect",function(){
            $tr.remove();
        })

}

})


